package com.elcl.widget.view.indictor;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.elcl.andbaselibrary.R;
import com.elcl.animation.AnimationConstant;
import com.elcl.util.ListUtils;

import java.util.List;

/**
 * Created by lichunlin on 17/6/20.
 * 类型选择的指示器
 */

public class TypeChooseIndictor extends CardView {

    private Context context;
    private Integer typeResourceId;

    public TypeChooseIndictor(Context context) {
        super(context);
        this.context = context;
    }

    public TypeChooseIndictor(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TypeChooseIndictor);
        typeResourceId = ta.getResourceId(R.styleable.TypeChooseIndictor_indictor_background, 0);
        ta.recycle();
    }


    public TypeChooseIndictor(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TypeChooseIndictor);
        typeResourceId = ta.getResourceId(R.styleable.TypeChooseIndictor_indictor_background, 0);
        ta.recycle();
    }

    private View indictorView = null;//指针view
    private int currentPosition = 0;//当前点击的postion
    private int lastPosition = 0;//上次点击的postion
    private List<View> childViews;//所有的childview
    private int sizeLast = 0;//第一次绘制的时候,indictorview的
    private int indictorColor = 000000;//指针的颜色
    private int textPressColor = 0;//点击textview的color

    public void init(final List<View> childViews) {
        this.childViews = childViews;
        if (ListUtils.isNotEmpty(childViews)) {
            initBackGroundView();
            initIndictorView();
            initChildView();
        }
    }

    //初始化view
    private void initBackGroundView() {
        setBackgroundResource(typeResourceId);
        setCardElevation(0);
    }

    //初始化指针的view
    private void initIndictorView() {
        if (null == indictorView) {
            indictorView = new View(context);
            indictorView.setBackgroundColor(indictorColor);
        }
        addView(indictorView);

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {//监听tagview绘制完成，重新绘制大小
            @Override
            public void onGlobalLayout() {
                if (sizeLast != getWidth() / childViews.size()) {//如果setLayoutParams就重绘制，会一直无限绘制下去，如果不相等的时候，采取绘制
                    indictorView.setLayoutParams(new FrameLayout.LayoutParams(getWidth() / childViews.size(), getHeight()));
                }
                sizeLast = getWidth() / childViews.size();

            }
        });
    }

    private void initChildView() {
        LinearLayout linearLayout = getLinearLayout(childViews);
        addView(linearLayout);
    }

    //将childviews添加到Linearlayout
    public LinearLayout getLinearLayout(final List<View> childViews) {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setBackgroundColor(00000000);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        for (int index = 0; index < childViews.size(); index++) {
            final View childView = childViews.get(index);
            linearLayout.addView(childView);

            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) childView.getLayoutParams();
            layoutParams.width = 0;
            layoutParams.weight = 1;
            childView.setLayoutParams(layoutParams);

            final int finalIndex = index;
            childView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickIndictorView(finalIndex);
                    lastPosition = currentPosition;

                    currentPosition = finalIndex;

                    if (null != onIndictorListener) {
                        onIndictorListener.onIndictorClickListener(currentPosition);
                    }
                    changeTextColorByClick(childView);
                }
            });
        }
        return linearLayout;
    }


    //改变字体的颜色
    private void changeTextColorByClick(View childView) {
        TextView textView = null;
        if (childView instanceof TextView) {
            textView = (TextView) childView;
            textView.setTextColor(textPressColor);

            View view = childViews.get(lastPosition);
            if (view instanceof TextView) {
                TextView textViewLast = (TextView) childView;
                textViewLast.setTextColor(getResources().getColor(R.color.white));
            }
        } else if (childView instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) childView;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View view = viewGroup.getChildAt(i);
                if (view instanceof TextView) {
                    textView = (TextView) view;
                    textView.setTextColor(getResources().getColor(R.color.white));
                    break;
                }
            }

            viewGroup = (ViewGroup) childViews.get(lastPosition);
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View view = viewGroup.getChildAt(i);
                if (view instanceof TextView) {
                    textView = (TextView) view;
                    textView.setTextColor(textPressColor);
                    break;
                }
            }
        }
    }

    //点击indictorview的监听
    private void clickIndictorView(int index) {
        float startX = childViews.get(currentPosition).getLeft();
        float endX = childViews.get(index).getLeft();

        translateAnimtion(startX, endX);
    }

    private void translateAnimtion(float startX, float endX) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(indictorView, AnimationConstant.TRANSLATION_X, startX, endX);
        animator.setDuration(300);
        animator.getTarget();
        animator.start();
    }


    private IndictorClickListener onIndictorListener;

    public void setOnIndictorListener(IndictorClickListener onIndictorListener) {//点击click中indictor的监听
        this.onIndictorListener = onIndictorListener;

    }

    public interface IndictorClickListener {
        void onIndictorClickListener(int position);
    }


    public View getIndictorView() {
        return indictorView;
    }

    public void setIndictorView(View indictorView) {
        this.indictorView = indictorView;
    }

    public int getIndictorColor() {
        return indictorColor;
    }

    public void setIndictorColor(int indictorColor) {
        this.indictorColor = indictorColor;
    }

    public int getTextPressColor() {
        return textPressColor;
    }

    public void setTextPressColor(int textPressColor) {
        this.textPressColor = textPressColor;
    }

}
